import { BaseService } from '@cool-midway/core';
import { Provide } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { HelpInfoEntity } from '../entity/info';

/**
 * 帮助信息服务
 */
@Provide()
export class HelpInfoService extends BaseService {
  @InjectEntityModel(HelpInfoEntity)
  helpInfoEntity: Repository<HelpInfoEntity>;

  /**
   * 根据类型获取帮助信息
   * @param type 类型：1-帮助中心，2-隐私保护说明，3-关于我们
   */
  async getByType(type: number) {
    return await this.helpInfoEntity.find({
      where: { type, status: 1 },
      order: { orderNum: 'ASC', createTime: 'ASC' },
    });
  }

  /**
   * 获取所有类型的帮助信息
   */
  async getAllTypes() {
    const result = {};
    for (let type = 1; type <= 3; type++) {
      const data = await this.getByType(type);
      const typeName = type === 1 ? 'helpCenter' : type === 2 ? 'privacyPolicy' : 'aboutUs';
      result[typeName] = data;
    }
    return result;
  }

  /**
   * 获取单条信息详情
   * @param id 信息ID
   */
  async getDetail(id: number) {
    return await this.helpInfoEntity.findOne({
      where: { id, status: 1 },
    });
  }
}